﻿using System;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using System.Drawing;
using Silvermoon.Shapes;
using Silvermoon.OpenGL;
using Silvermoon.Images;

namespace Silvermoon.Controls.Sys
{
    internal class KeyboardLayout
    {
        private static readonly Texture defShift = SysImages.Shift;
        private static readonly Texture defBack = SysImages.Backspace;
        private static readonly Texture defEnter = SysImages.Enter;

        public string Row1;
        public string Row2;
        public string Row3;

        public object Shift = defShift;
        public object BackSpace = defBack;
        public object Ctrl;
        public object Return = defEnter;
        public string Space = "space";
        public string Comma = ",";
        public string Point = ".";

        public void LayoutRows(Keyboard panel, KeyButton shift, KeyButton back, KeyButton ctrl, KeyButton enter)
        {
            LayoutRow(panel, Row1, 0);
            LayoutRow(panel, Row2, 1);
            LayoutRow3(panel, shift, back);
            LayoutRow4(panel, ctrl, enter);
        }

        private void LayoutRow4(Keyboard panel, KeyButton ctrl, KeyButton enter)
        {
            int buttonSpace = panel.ButtonSpace;
            int keyHeight = panel.ButtonHeight;
            int keyWidth = panel.ButtonWidth;
            ctrl.Content = Shape.FromObject(this.Ctrl);
            enter.Content = Shape.FromObject(this.Return);

            int xs = buttonSpace / 2;
            int left = xs + ctrl.Width + buttonSpace;
            int top = xs + keyHeight * 3;

            ctrl.Location = new Point(xs, top);
            enter.Location = new Point(xs + panel.Width - enter.Width - buttonSpace, top);
            KeyButton comma = panel.GetButton();
            KeyButton point = panel.GetButton();
            KeyButton space = panel.GetButton();
            space.IsSpace = true;

            comma.Keys = Comma;
            point.Keys = Point;
            space.Keys = " ";

            comma.Content = this.Comma;
            point.Content = this.Point;
            space.Content = this.Space;

            comma.Location = new Point(left, top);
            left += keyWidth;
            space.Width = 4 * keyWidth - buttonSpace;
            space.Location = new Point(left, top);
            left += 4 * keyWidth;
            point.Location = new Point(left, top);

        }

        private void LayoutRow3(Keyboard panel, KeyButton shift, KeyButton back)
        {
            int buttonSpace = panel.ButtonSpace;
            int keyHeight = panel.ButtonHeight;
            int keyWidth = panel.ButtonWidth;

            shift.Content = Shape.FromObject(Shift);
            back.Content = Shape.FromObject(this.BackSpace);
            int n = Row3.Length;
            int xs = buttonSpace / 2;
            int left = (panel.Width - keyWidth * n) / 2 + xs;
            int top = panel.ButtonSpace / 2 + keyHeight * 2;

            shift.Location = new Point(xs, top);
            back.Location = new Point(xs + panel.Width - back.Width - buttonSpace, top);
            foreach (char c in Row3)
            {
                KeyButton btn = panel.GetButton();
                btn.Keys = c.ToString();
                btn.Content = c.ToString();
                btn.Location = new Point(left, top);
                left += keyWidth;
            }
        }

        private void LayoutRow(Keyboard panel, string row, int y)
        {
            int keyHeight = panel.ButtonHeight;
            int keyWidth = panel.ButtonWidth;
            int space = panel.ButtonSpace;

            int n = row.Length;
            int left = (panel.Width - keyWidth * n) / 2 + space / 2;
            int top = space / 2 + keyHeight * y;
            foreach (char c in row)
            {
                KeyButton btn = panel.GetButton();
                btn.Keys = c.ToString();
                btn.Content = c.ToString();
                btn.Location = new Point(left, top);
                left += keyWidth;
            }
        }
    }


    public enum KeyboardState
    {
        None,
        Shift,
        Control,
        ShiftControl
    }

    internal struct KeyboardLayouts
    {
        public KeyboardLayout Default;
        public KeyboardLayout Shift;
        public KeyboardLayout Control;
        public KeyboardLayout ShiftControl;
    }
}
